﻿using System.Text.Json;
using System.Text.Json.Serialization;

namespace StudyCompanion.Commons.Converter;

/// <summary>
///     long型数据转换
/// </summary>
public class LongJsonConverter : JsonConverter<long>
{
    /// <summary>
    /// </summary>
    /// <param name="reader"></param>
    /// <param name="typeToConvert"></param>
    /// <param name="options"></param>
    /// <returns></returns>
    public override long Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
    {
        if (reader.TokenType == JsonTokenType.Number)
            return reader.GetInt64();
        if (!long.TryParse(reader.GetString(), out var value))
            return value;
        return 0L;
    }

    /// <summary>
    ///     long数据在前端会出现丢失精度，故转换为string类型
    /// </summary>
    /// <param name="writer"></param>
    /// <param name="value"></param>
    /// <param name="options"></param>
    public override void Write(Utf8JsonWriter writer, long value, JsonSerializerOptions options)
    {
        writer.WriteStringValue(value.ToString());
    }
}